package com.haoniu.common.utils.huanxin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haoniu.common.config.Global;
import com.haoniu.common.exception.HuanXinExcetion;
import com.haoniu.common.utils.SysPropUtils;
import com.haoniu.common.utils.YamlUtil;
import com.haoniu.common.utils.huanxin.api.*;
import com.haoniu.common.utils.huanxin.comm.ClientContext;
import com.haoniu.common.utils.huanxin.comm.EasemobRestAPIFactory;
import com.haoniu.common.utils.huanxin.comm.body.*;
import com.haoniu.common.utils.huanxin.comm.constant.MsgType;
import com.haoniu.common.utils.huanxin.comm.wrapper.BodyWrapper;
import com.haoniu.common.utils.huanxin.comm.wrapper.ResponseWrapper;
import org.apache.http.conn.EofSensorInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.ws.Response;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

public class HuanXinUtils {
	static Logger logger = LoggerFactory.getLogger(HuanXinUtils.class);

	private static EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
	private static IMUserAPI user = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
	private static ChatMessageAPI chat = (ChatMessageAPI)factory.newInstance(EasemobRestAPIFactory.MESSAGE_CLASS);
	private static FileAPI file = (FileAPI)factory.newInstance(EasemobRestAPIFactory.FILE_CLASS);
	private static SendMessageAPI message = (SendMessageAPI)factory.newInstance(EasemobRestAPIFactory.SEND_MESSAGE_CLASS);
	private static ChatGroupAPI chatgroup = (ChatGroupAPI)factory.newInstance(EasemobRestAPIFactory.CHATGROUP_CLASS);
	private static ChatRoomAPI chatroom = (ChatRoomAPI)factory.newInstance(EasemobRestAPIFactory.CHATROOM_CLASS);

	/**
	 *  环信用户Id后缀
	 */
	public static String PROJECT_ID ;
	private static String ENV = "dev";

	static {
		ENV = Global.getConfig("spring.profiles.active");
		PROJECT_ID = YamlUtil.getProperty("application-" + ENV + ".yml","huanxin.project_id");
		System.out.println(PROJECT_ID);
	}

	/**
	 * 功能描述:注册用户信息
	 *
	 * @author yangliu  2016年7月26日 下午5:12:59
	 *
	 * @param userId 用户名
	 * @param password 密码
	 * @param nickName 昵称
	 */
	public static void createUser(String userId, String password, String nickName){
		userId = userId+PROJECT_ID;
		BodyWrapper payload = new IMUserBody(userId, password, nickName);
		ResponseWrapper object=(ResponseWrapper) user.createNewIMUserBatch(payload);
		if(object.getResponseStatus()!=200){
			logger.error("环信注册失败："+object.getResponseStatus()+",userId:"+userId);
			throw new HuanXinExcetion("用户注册失败");
		}
	}

	/**
	 * 修改环信昵称
	 *
	 * @param userId
	 * @param nickName
	 */
	public static void updateIMUserNickName(String userId, String nickName){
		userId = userId+PROJECT_ID;
		BodyWrapper payload = new IMUserBody(userId,HuanXinConstants.HAUNXIN_USER_PASSWORD_DETAULT,nickName);
		ResponseWrapper object=(ResponseWrapper) user.modifyIMUserNickNameWithAdminToken(userId,payload);
		if(object.getResponseStatus()!=200){
			logger.error("环信昵称修改失败："+object.getResponseStatus()+",userId:"+userId);
			throw new HuanXinExcetion("昵称修改失败");
		}
	}


	/**
	 * 功能描述:删除用户失败
	 *
	 * @author yangliu  2016年7月26日 下午5:36:32
	 *
	 * @param userNum 用户编号
	 */
	public static void deleteUser(String userNum){
		userNum = userNum+PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) user.deleteIMUserByUserName(userNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除用户失败："+object.getResponseStatus()+",userName:"+userNum);
			throw new HuanXinExcetion("删除用户失败");
		}
	}

	/**
	 * 强制下线
	 * @param userName
	 */
	public static void disconnectIMUser(String userName){
		userName = userName+PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) user.disconnectIMUser(userName);
		if(object.getResponseStatus()!=200){
			logger.error("环信强制下线失败："+object.getResponseStatus()+",userName:"+userName);
			throw new HuanXinExcetion("环信强制下线失败");
		}
	}

	/**
	 * 功能描述: 添加好友
	 *
	 * @author yangliu  2016年7月26日 下午5:39:47
	 *
	 * @param userNum
	 * @param friendNum
	 */
	public static void addFriendSingle(String userNum, String friendNum){
		userNum = userNum+ PROJECT_ID;
		friendNum = friendNum+ PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) user.addFriendSingle(userNum,friendNum);

		System.out.println(object.toString());
		if(object.getResponseStatus()!=200){
			logger.error("环信添加好友："+object.getResponseStatus()+",userName:"+userNum+",friendNum:"+friendNum);
			throw new HuanXinExcetion("添加好友失败");
		}
	}

	/**
	 * 获取用户好友
	 * @param userNum
	 * return A,B,C,D
	 */
	public static String getFriends(String userNum){
		userNum = userNum + PROJECT_ID;
		ResponseWrapper object= (ResponseWrapper)user.getFriends(userNum);

		if(object.getResponseStatus()!=200){
			logger.error("环信获取好友："+object.getResponseStatus()+",userName:"+userNum);
			throw new HuanXinExcetion("获取好友失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(object.getResponseBody().toString());
		String data = jsonObject.get("data").toString();
		if (data == null || data == ""){
			return "";
		}
		return data.substring(1,data.length()-1);
	}


	/**
	 * 功能描述:删除好友
	 *
	 * @author yangliu  2016年7月26日 下午5:39:47
	 *
	 * @param userNum
	 * @param friendNum
	 */
	public static void deleteFriendSingle(String userNum, String friendNum){
		userNum = userNum+ PROJECT_ID;
		friendNum = friendNum+ PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) user.deleteFriendSingle(userNum,friendNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除好友："+object.getResponseStatus()+",userName:"+userNum+",friendNum:"+friendNum);
			throw new HuanXinExcetion("删除好友失败");
		}
	}

	/**
	 * 往IM用户的黑名单中加人 <br>
	 * POST
	 *
	 * @ userName
	 *            用戶名或用戶ID
	 * @ payload
	 *            <code>{"usernames":["5cxhactgdj", "mh2kbjyop1"]}</code>
	 * @return
	 */
	public static void addToBlackList(String userNum, String[] friendNums){
		userNum = userNum + PROJECT_ID;
		UserNamesBody payload=new UserNamesBody(friendNums);
		ResponseWrapper object=(ResponseWrapper) user.addToBlackList(userNum, payload);
		if(object.getResponseStatus()!=200){
			logger.error("环信加入黑名单："+object.getResponseStatus()+",userName:"+userNum+",friendNums:"+friendNums);
			throw new HuanXinExcetion("添加黑名单失败");
		}
	}

	/**
	 * 从IM用户的黑名单中减人 <br>
	 * DELETE
	 *
	 * @ userName
	 *            用戶名或用戶ID
	 * @ blackListName
	 *            黑名单用戶名或用戶ID
	 * @return
	 */
	public static void deleteFromBlackList(String userNum, String friendNum){
		userNum = userNum+ PROJECT_ID;
		friendNum = friendNum+ PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) user.removeFromBlackList(userNum, friendNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除黑名单："+object.getResponseStatus()+",userName:"+userNum+",friendNum:"+friendNum);
			throw new HuanXinExcetion("删除黑名单失败");
		}
	}
	/**
	 * 功能描述: 创建群组并返回群组编号
	 *
	 * @author yangliu  2016年7月27日 上午9:34:22
	 *
	 * @param userNum 用户编号
	 * @param groupName 群名称
	 * @param otherUserNum 添加的其他组员编号 ["zhangsan","lisi"]
	 * @return
	 */
	public static String addGroup(String userNum, String groupName, String[] otherUserNum){
		userNum = userNum+ PROJECT_ID;
		ChatGroupBody payload = new ChatGroupBody(groupName, groupName, false, 2000L,false , userNum, otherUserNum);
		ResponseWrapper object=(ResponseWrapper) chatgroup.createChatGroup(payload);
		if(object.getResponseStatus()!=200){
			logger.error("环信创建群失败："+object.getResponseStatus()+",userName:"+userNum+",groupName:"+groupName);
			throw new HuanXinExcetion(String.valueOf(object.getResponseBody()));
		}
		JSONObject jsonObject=JSONObject.parseObject(object.getResponseBody().toString());
		JSONObject data=jsonObject.getJSONObject("data");
		return data.getString("groupid");
	}

	/**
	 * 创建聊天室
	 */
	public static String addRoom(String userNum, String groupName, String[] otherUserNum){
		userNum = userNum+ PROJECT_ID;
		ChatRoomBody chatRoomBody = new ChatRoomBody(groupName,groupName,5000L,userNum, otherUserNum);
		ResponseWrapper object = (ResponseWrapper) chatroom.createChatRoom(chatRoomBody);
		if (object.getResponseStatus()!=200){
			logger.error("环信创建聊天室失败："+object.getResponseStatus()+"，userName"+userNum+"，groupName:"+groupName);
			throw new HuanXinExcetion(String.valueOf(object.getResponseBody()));
		}
		JSONObject jsonObject = JSONObject.parseObject(object.getResponseBody().toString());
		JSONObject data = jsonObject.getJSONObject("data");
		return data.getString("id");
	}


	/**
	 * 获取群详情
	 * @param groupIds
	 */
	public static void getChatGroupDetails(String[] groupIds){
		ResponseWrapper object=(ResponseWrapper) chatgroup.getChatGroupDetails(groupIds);
		if(object.getResponseStatus()==404){
			logger.error("环信获取群组详情失败："+object.getResponseStatus()+",groupId:"+groupIds);
			throw new HuanXinExcetion("环信获取群组详情失败");
		}
	}

	/**
	 * 获取一个聊天室详情
	 */
	public static void getChatRoomDetails(String groupId){
		ResponseWrapper object = (ResponseWrapper) chatroom.getChatRoomDetail(groupId);
		if (object.getResponseStatus()==404){
			logger.error("环信获取聊天室详情失败："+object.getResponseStatus()+"，groupId"+groupId);
			throw new HuanXinExcetion("环信获取聊天室详情失败");
		}
	}

	/**
	 * 功能描述: 修改群信息
	 *
	 * @author yangliu  2016年8月13日 下午5:14:35
	 *
	 * @param groupNum 群编号
	 * @param groupName 群名称
	 * @return
	 */
	public static void updateGroup(String groupNum, String groupName){
		ModifyChatGroupBody groupBody=new ModifyChatGroupBody(groupName, groupName, 2000L);
		ResponseWrapper object=(ResponseWrapper) chatgroup.modifyChatGroup(groupNum, groupBody);
		if(object.getResponseStatus()!=200){
			logger.error("修改群名片："+object.getResponseStatus()+",groupNum:"+groupNum+",groupName:"+groupName);
			throw new HuanXinExcetion("修改群名片");
		}
	}

	/**
	 * 修改聊天室信息
	 */
	public static void updateRoom(String roomNum,String roomName){
		ModifyChatRoomBody roomBody = new ModifyChatRoomBody(roomName,roomName,5000L);
		ResponseWrapper object = (ResponseWrapper) chatroom.modifyChatRoom(roomNum,roomBody);
		if (object.getResponseStatus()!=200){
			logger.error("修改聊天室名称："+object.getResponseStatus()+"，roomNum："+roomNum+"，roomName"+roomName);
			throw new HuanXinExcetion("修改聊天室名称");
		}
	}

	/**
	 * 功能描述:环信删除群组
	 *
	 * @author yangliu  2016年7月27日 上午10:12:10
	 *
	 * @param groupId 群组ID
	 */
	public static void deleteGroup(String groupId){
		ResponseWrapper object=(ResponseWrapper) chatgroup.deleteChatGroup(groupId);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除群组失败："+object.getResponseStatus()+",groupId:"+groupId);
			throw new HuanXinExcetion("删除群组失败");
		}
	}

	/**
	 *
	 * 环信删除聊天室
	 */
	public static void deleteRoom(String groupId){
		ResponseWrapper object = (ResponseWrapper) chatroom.deleteChatRoom(groupId);
		if (object.getResponseStatus()!=200){
			logger.error("环信删除聊天室失败："+object.getResponseStatus()+"，groupId："+groupId);
			throw new HuanXinExcetion("删除聊天室失败");
		}
	}


	/**
	 * 功能描述: 给群组加人[单个]
	 *
	 * @author yangliu  2016年7月27日 上午9:36:55
	 *
	 * @param groupId
	 * @param userNum
	 */
	public static void addUserToGroup(String userNum, String groupId){
		userNum = userNum+ PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) chatgroup.addSingleUserToChatGroup(groupId,userNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信给群组加人失败："+object.getResponseStatus()+",userName:"+userNum+",groupId:"+groupId);
			throw new HuanXinExcetion("群组群组加人失败");
		}else{
			logger.info("环信群组加人成功！");
		}
	}
	/**
	 * 给聊天室添加人 单个
	 */
	public static void addUserToRoom(String userNum,String groupId){
		userNum = userNum+PROJECT_ID;
		ResponseWrapper object = (ResponseWrapper) chatroom.addSingleUserToChatRoom(groupId,userNum);
		if (object.getResponseStatus()!=200){
			logger.error("环信给聊天室加人失败："+object.getResponseStatus()+"，userName"+userNum+"，groupId："+groupId);
			throw new HuanXinExcetion("环信聊天室加人失败");
		}else {
			logger.info("环信聊天室加人成功！");
		}
	}

	/**
	 * 功能描述:给群组加人[多个]
	 *
	 * @author yangliu  2016年7月27日 上午9:59:50
	 *
	 * @param userNums 数组
	 * @param groupId
	 */
	public static void addUserToGroup(String[] userNums, String groupId){
		if(userNums==null){
			return;
		}
		for (int i=0;i<userNums.length;i++) {
			String userNum = userNums[i];
			userNums[i] = userNum+PROJECT_ID;
		}
		UserNamesBody userNames=new UserNamesBody(userNums);
		ResponseWrapper object=(ResponseWrapper) chatgroup.addBatchUsersToChatGroup(groupId,userNames);
		if(object.getResponseStatus()!=200){
			logger.error("环信给群组添加好友失败："+object.getResponseStatus()+",userName:"
					+JSONArray.toJSONString(userNames)+",groupId:"+groupId);
			throw new HuanXinExcetion("选择用户不符合要求");
		}
	}
	/**
	 * 给聊天室加多个人
	 */
	public static void addUserToRoom(String[] userNums,String groupId){
		if (userNums==null){
			return;
		}
		for (int i=0;i<userNums.length;i++) {
			String userNum = userNums[i];
			userNums[i] = userNum+PROJECT_ID;
		}
		UserNamesBody userNames=new UserNamesBody(userNums);
		ResponseWrapper object = (ResponseWrapper) chatroom.addBatchUsersToChatRoom(groupId,userNames);
		if(object.getResponseStatus()!=200){
			logger.error("环信给聊天室添加好友失败："+object.getResponseStatus()+",userName:"
					+JSONArray.toJSONString(userNames)+",groupId:"+groupId);
			throw new HuanXinExcetion("选择用户不符合要求");
		}
	}

	/**
	 * 功能描述: 把用户从群组里面删除
	 *
	 * @author yangliu  2016年7月27日 上午10:04:11
	 *
	 * @param userNum 用户编号
	 * @param groupId 群组ID
	 */
	public static void deleteUserFromGroup(String userNum, String groupId){
		userNum = userNum+ PROJECT_ID;
		ResponseWrapper object=(ResponseWrapper) chatgroup.removeSingleUserFromChatGroup(groupId,userNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除群组成员失败："+object.getResponseStatus()+",userName:"+userNum+",groupId:"+groupId);
			throw new HuanXinExcetion("删除群组成员失败");
		}
	}

	/**
	 * 把用户从聊天室删除
	 */
	public static void deleteUserFromRoom(String userNum,String groupId){
		userNum = userNum+ PROJECT_ID;
		ResponseWrapper object = (ResponseWrapper) chatroom.removeSingleUserFromChatRoom(groupId,userNum);
		if (object.getResponseStatus()!=200){
			logger.error("环信删除聊天室成员失败："+object.getResponseStatus()+",userName:"+userNum+",groupId:"+groupId);
			throw new HuanXinExcetion("删除聊天室成员失败");
		}
	}

	/**
	 * 功能描述: 把用户从群组里面删除[多个]
	 *
	 * @author yangliu  2016年7月27日 上午10:04:11
	 *
	 * @param userNum 用户编号
	 * @param groupId 群组ID
	 */
	public static void deleteUserFromGroup(String[] userNum, String groupId){
		ResponseWrapper object=(ResponseWrapper) chatgroup.removeBatchUsersFromChatGroup(groupId,userNum);
		if(object.getResponseStatus()!=200){
			logger.error("环信删除群组成员批量失败："+object.getResponseStatus()+",userName:"+userNum+",groupId:"+groupId);
			throw new HuanXinExcetion("删除群组成员批量失败");
		}
	}

	/**
	 * 把用户从聊天室删除[多个]
	 */
	public static void deleteUserFromRoom(String [] userNum,String groupId){
		ResponseWrapper object = (ResponseWrapper) chatroom.removeBatchUsersFromChatRoom(groupId,userNum);
		if (object.getResponseStatus()!=200){
			logger.error("环信删除聊天室成员批量失败："+object.getResponseStatus()+",userName:"+userNum+",groupId:"+groupId);
			throw new HuanXinExcetion("删除聊天室成员批量失败");
		}
	}

	/**
	 * 功能描述:给用户发文本消息[单个]
	 *
	 * @author yangliu  2016年7月27日 上午11:20:46
	 *
	 * @param userNum 发送者
	 * @param msg 文本
	 * @param toUser 用户[单个]
	 */
	public static void sendUserTextMessage(String userNum, String msg, String toUser){
		toUser = toUser+ PROJECT_ID;
		TextMessageBody msgBody = new TextMessageBody("users", new String[]{toUser}, userNum, null, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("用户发送消息失败");
		}
	}

	public static void sendUserTextMessage(String userNum, String msg, String toUser, Map<String, String> ext){
		toUser = toUser+ PROJECT_ID;
		TextMessageBody msgBody = new TextMessageBody("users", new String[]{toUser}, userNum, ext, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("用户发送消息失败");
		}
	}

	/**
	 * 给用户发送透传消息【单个】
	 * @param userNum	发送者
	 * @param msg		文本
	 * @param toUser	用户[单个]
	 * @param ext		扩展
	 */
	public static void sendUserCmdMessage(String userNum, String msg, String toUser, Map<String, String> ext){
		toUser = toUser+ PROJECT_ID;
		CmdMessageBody msgBody = new CmdMessageBody("users", new String[]{toUser}, userNum, ext, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("用户发送消息失败");
		}
	}

	/**
	 * 功能描述:给用户发文本消息[多个]
	 *
	 * @author yangliu  2016年7月27日 上午11:20:46
	 *
	 * @param userNum 发送者
	 * @param msg 文本
	 * @param toUsers 用户[多个]
	 */
	public static void sendUserTextMessage(String userNum, String msg, String[] toUsers,Map<String, String> ext){
		userNum = userNum + PROJECT_ID;
		for (int i=0;i<toUsers.length;i++){
			toUsers[i]=toUsers[i]+PROJECT_ID;
		}
		TextMessageBody msgBody = new TextMessageBody("users", toUsers, userNum, ext, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("用户发送消息失败");
		}
	}

	public static void sendUserTextMessage(String userNum, String msg, String[] toUsers){
		TextMessageBody msgBody = new TextMessageBody("users", toUsers, userNum, null, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("用户发送消息失败");
		}
	}

	/**
	 * 功能描述:给群发文本消息[单个]
	 *
	 * @author yangliu  2016年7月27日 上午11:20:46
	 *
	 * @param userNum 发送者
	 * @param msg 文本
	 * @param groupId 用户[单个]
	 */
	public static void sendGroupTextMessage(String userNum, String msg, String groupId){
		TextMessageBody msgBody = new TextMessageBody("chatgroups", new String[]{groupId}, userNum, null, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("群发送消息失败");
		}
	}

	public static void sendGroupTextMessage(String userNum, String msg, String groupId, Map<String, String> ext){
		ext.put("message",msg);

		ResponseWrapper object;
		if (MsgType.CMD.equals(HuanXinConstants.HUANXIN_MSG_TYPE)){
			CmdMessageBody msgBody = new CmdMessageBody("chatgroups", new String[]{groupId}, userNum, ext, msg);
			object=(ResponseWrapper) message.sendMessage(msgBody);
		}else {
			TextMessageBody msgBody = new TextMessageBody("chatgroups", new String[]{groupId}, userNum, ext, msg);
			object=(ResponseWrapper) message.sendMessage(msgBody);
		}


		if(object.getResponseStatus()!=200){
			logger.error("环信给用户发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("群发送消息失败");
		}
	}


	/**
	 * 功能描述:给群发文本消息[多个]
	 *
	 * @author yangliu  2016年7月27日 上午11:20:46
	 *
	 * @param userNum 发送者
	 * @param msg 文本
	 * @param groupIds 用户[多个]
	 */
	public static void sendGroupTextMessage(String userNum, String msg, String[] groupIds){
		TextMessageBody msgBody = new TextMessageBody("chatgroups", groupIds, userNum, null, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给群组发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("批量群组发送消息失败");
		}
	}

	public static void sendGroupTextMessage(String userNum, String msg, String[] groupIds, Map<String, String> ext){
		TextMessageBody msgBody = new TextMessageBody("chatgroups", groupIds, userNum, ext, msg);
		ResponseWrapper object=(ResponseWrapper) message.sendMessage(msgBody);
		if(object.getResponseStatus()!=200){
			logger.error("环信给群组发送消息失败："+object.getResponseStatus()+",userName:"+userNum+",msg:"+msg);
			throw new HuanXinExcetion("批量群组发送消息失败");
		}
	}

	/**
	 * 功能描述:获取消息聊天记录
	 *
	 * @author yangliu  2016年8月10日 上午11:39:18
	 *
	 * @param timestamp 时间
	 * @return
	 */
	public static JSONArray getChatMessages(long timestamp){
		String sql=String.format("timestamp>%s", timestamp);
		ResponseWrapper object=(ResponseWrapper)chat.exportChatMessages(1000L,"", sql);
		if(object.getResponseStatus()!=200){
			logger.error("环信获取消息失败"+object.getResponseStatus()+",timestamp:"+timestamp);
			throw new HuanXinExcetion("获取消息失败");
		}
		JSONArray jsonObject=JSONObject.parseObject(object.getResponseBody().toString()).getJSONArray("entities");
		System.out.println(jsonObject.toString());
		return jsonObject;
	}

	/**
	 * 功能描述:下载文件
	 *
	 * @author yangliu  2016年8月10日 上午10:49:57
	 *
	 * @param fileUUID
	 * @param shareSecret
	 * @param isThumbnail
	 * @return
	 */
	public static EofSensorInputStream loadFile(String fileUUID, String shareSecret, Boolean isThumbnail){
		ResponseWrapper object=(ResponseWrapper)file.downloadFile(fileUUID, shareSecret, isThumbnail);
		if(object.getResponseStatus()!=200){
			logger.error("环信下载文件失败"+object.getResponseStatus()+",fileUUID:"+fileUUID+",shareSecret:"+shareSecret);
			throw new HuanXinExcetion("下载文件失败");
		}
		EofSensorInputStream eof=(EofSensorInputStream)object.getResponseBody();
		return eof;
	}
}
